<template>
  <div class="app-container">
    <!--    <el-row :gutter="20">-->
    <!-- 安排列表 -->
    <!--      <el-col :lg="24" :xs="24">-->
    <div class="toolbar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="课程名称" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="分类名称"
            clearable
            style="width: 300px;height: 30px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="教练名称" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="分类名称"
            clearable
            style="width: 300px;height: 30px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="场地名称" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="分类名称"
            clearable
            style="width: 300px;height: 30px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="开始时间" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="分类名称"
            clearable
            style="width: 300px;height: 30px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size='large' @click="handleQuery" :icon='Search'
          >搜索</el-button
          >
          <el-button @click="resetQuery" size='large' :icon='Refresh'>
            重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="pageData"
      :strip='true'
      :border='true'
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        key="courseName"
        label="课程名称"
        align="center"
        prop="courseName"
        width="200"
      />
      <el-table-column
        key="name"
        label="安排名称"
        align="center"
        prop="name"
        width="200"
      />
      <el-table-column
        label="课程教练"
        align="center"
        prop="coachName"
        width="150"
      />
      <el-table-column
        label="课程场地"
        align="center"
        prop="venueName"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        label="开课状态"
        align="center"
        width="100"
        prop="status"
      />
      <el-table-column
        label="开始时间"
        align="center"
        prop="startTime"
        width="180"
      />
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
      />
      <el-table-column label="操作" align="center" width="450">
        <template #default="scope">
          <el-button
            type="success"
            size="large"
            @click="openDialog(scope.row.id)"
            :icon='ZoomIn'
          >查看详情</el-button>
          <el-button
            type="warning"
            size="large"
            @click="handleDelete(scope.row.id)"
            :icon='Delete'
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-footer class='footer'>
      <el-pagination
        large
        background
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        :page-count='pagination.pageCount'
        layout='total, prev, pager, next'
        :total='pagination.total'
        class='mt-4'
        style='float: right'
        @change='getPage'
      />
    </el-footer>

  </div>
</template>

<script setup lang='ts'>
import {
  getCourseScheduleForm,
  getCourseSchedulePage,
  deleteCourseSchedule
} from "@/api/course/schedule";
import { Plus, Delete, Edit, Search, Share, Refresh, ZoomIn } from '@element-plus/icons-vue'
import { type UploadInstance, ElMessageBox, ElMessage, ElForm } from 'element-plus'
import { ref,onMounted,reactive } from "vue";
import type { CourseScheduleForm, CourseScheduleQueryParams, CourseScheduleVO } from '@/api/course/schedule/types.ts'
import { useRouter } from 'vue-router'

const queryFormRef = ref(ElForm); // 查询表单

const loading = ref(false); //  加载状态
const removeIds = ref([]); // 删除用户ID集合 用于批量删除
const queryParams = reactive<CourseScheduleQueryParams>({
  pageNum: 1,
  pageSize: 10,
});
const total = ref(0); // 数据总数
const pageData = ref<CourseScheduleVO[]>(); // 分页数据
const pagination = ref({
  currentPage: 1,pageSize: 10,total: 0,pageCount: 1
});
const router = useRouter()
/** 查询 */
function handleQuery() {
  getPage(queryParams.pageNum,queryParams.pageSize);
}

function getPage(currentPage:number,pageSize :number){
  pagination.value.currentPage = queryParams.pageNum = currentPage || 1;
  pagination.value.pageSize = queryParams.pageSize = pageSize || 10
  loading.value = true;
  getCourseSchedulePage(queryParams)
    .then(({ data }) => {
      pageData.value = data.records;
      total.value = data.total;
      pagination.value.currentPage = data.current
      pagination.value.pageSize = data.size
      pagination.value.total = data.total
      pagination.value.pageCount = data.pages
      queryParams.pageNum = data.current;
      queryParams.pageSize = data.size;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行选中 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/**
 * 打开弹窗
 *
 * @param id 用户ID
 */
async function openDialog(id: number) {
  await router.push({ name: 'schedule-detail', state: { scheduleId: id } })
}


/** 删除 */
function handleDelete(id: number) {
  ElMessageBox.confirm("确认删除分类信息?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    deleteCourseSchedule(id).then(() => {
      ElMessage.success("删除成功");
      resetQuery();
    });
  });
}

onMounted(() => {
  handleQuery();
});
</script>
.toolbar {
padding: 10px 0;
}
<style scoped>
.footer {
  margin-top: 20px;
}
</style>
